<?php

class LogisticsService extends BaseService
{
    private static $self = NULL;
    private $userInfo = NULL;

    /**
     * @return LogisticsService|null
     */
    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }


    /**
     * @var null
     */
    private $mLogisticsLine     = NULL;


    /**
     * 实例化model
     */
    public function __construct()
    {
        $this->mLogisticsLine          = new Involved_LogisticsLineModel();
    }


    /**
     * 参数验证
     * @param $obj
     * @param $arr
     * @author alice
     * @date 2016-1-19
     * @return bool
     */
    public function  checkProperties($obj, $arr)
    {
        if (empty($arr)) return true;
        $obj = (array)$obj;

        foreach ($arr as $v) {
            if (empty($obj[$v])) {
                return false;
            }
        }
        return true;
    }


    /**
     * 物流信息列表
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowLogisticsList()
    {
        return $this->mLogisticsLine->ShowLogisticsList();
    }

    /**
     * 物流线路详情
     * @param $id
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowLogisticsInfo($id)
    {
        return $this->mLogisticsLine->ShowLogisticsInfo($id);
    }

    /**
     * 添加物流线路
     * @param $paramObj
     * @author alice
     * @date 2016-1-19
     * @return int
     * @throws Exception
     */
    public function AddLogistics($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['departure']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        $aData = [
            'departure'                    =>     $paramObj->departure,
            'destination'                  =>     $paramObj->destination,
            'model_type'                   =>     $paramObj->model_type,
            'truck_length'                 =>     $paramObj->truck_length,
            'truck_weight'                 =>     $paramObj->truck_weight,
            'take_time'                    =>     $paramObj->take_time,
            'spec_time'                    =>     $paramObj->spec_time,
            'created'                      =>     time(),
        ];

        return $this->mLogisticsLine->insert($aData);
    }

    /**
     * 修改物流线路
     * @param $paramObj
     * @author alice
     * @date 2016-1-19
     * @return bool
     */
    public function EditLogistics($paramObj)
    {
        $aData = [
            'departure'                    =>     $paramObj->departure,
            'destination'                  =>     $paramObj->destination,
            'model_type'                   =>     $paramObj->model_type,
            'truck_length'                 =>     $paramObj->truck_length,
            'truck_weight'                 =>     $paramObj->truck_weight,
            'take_time'                    =>     $paramObj->take_time,
            'spec_time'                    =>     $paramObj->spec_time,
            'created'                      =>     time(),
        ];

        return $this->mLogisticsLine->update($aData,['wl_id'=>$paramObj->wl_id]);
    }


}